/*
Package setting
@Time : 2022/12/20 21:31
@Author : 董胜烨
@File : setting
@Software: GoLand
@note:
*/
package setting

import (
	"github.com/go-ini/ini"
	"log"
	"time"
)

type App struct {
	RunMode string
}

type DataBase struct {
	Type     string
	User     string
	Password string
	Name     string
	Host     string
}
type Server struct {
	HttpPort     int
	WriteTimeout time.Duration
	ReadTimeout  time.Duration
}

var (
	Cfg *ini.File
	err error
)
var AppSetting = &App{}
var ServerSetting = &Server{}
var DataBaseSetting = &DataBase{}

func Setup() {
	Cfg, err = ini.Load("conf/app.ini")
	if err != nil {
		log.Fatalln("无法引入app.ini文件")
	}
	mapTo("app", AppSetting)
	mapTo("server", ServerSetting)
	mapTo("database", DataBaseSetting)

	ServerSetting.ReadTimeout = ServerSetting.ReadTimeout * time.Second
	ServerSetting.WriteTimeout = ServerSetting.WriteTimeout * time.Second
}

func mapTo(section string, v interface{}) {
	err = Cfg.Section(section).MapTo(v)
	if err != nil {
		log.Fatalf("Cfg.MapTo %s err:%v", section, err)
	}
}
